package main

import (
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/prometheus/log"
)

type tXMLConf struct {
	XMLName     xml.Name      `xml:"queue"`
	ResourceXML []ResourceXML `xml:"resource"`
}

type ResourceXML struct {
	Serial string `xml:"serial"`
	Name   string `xml:"name"`
	User   string `xml:"user"`
}

func (x *tXMLConf) ReadXML(path string) error {
	xmlFile, err := os.Open(path)
	if err != nil {
		log.Error("Error opening file: %s", path)
		return err
	}
	defer xmlFile.Close()
	data, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		log.Error("Error reading file: %s", path)
		return err
	}
	err = xml.Unmarshal(data, &x)
	if err != nil {
		log.Error("Error unmarshal xml.")
		return err
	}
	return nil
}

func main() {
	var t = tXMLConf{}
	t.ReadXML("schedule-conf.xml")
	fmt.Println(t)
	for _, v := range t.ResourceXML {
		fmt.Println(v)
	}
}
